<template>
	<view>
		<view class="container">
			<text class="text-lg">验证手机号</text>
			<view class="row input-phone" >
				<zywork-icon type="icon-shouji" size="40" color="#919191"></zywork-icon>
				<text class="text-base" style="margin-left: 15rpx;">+86</text>
				<input style="margin-left: 15rpx;" placeholder="请输入手机号" disabled type="number" v-model="phone"  pattern="[0-9]*"/>
			</view>
			
			<view  class="row input-phone" style="">
				<zywork-icon type="icon-yuechi" size="40" color="#919191"></zywork-icon>
				<input style="margin-left: 15rpx;width: 300rpx;" placeholder="请输入验证码" type="number" v-model="code"  pattern="[0-9]*"/>
				<text class="text-base " :class="getCodeDisabled?'font-color1':'font-color'" style="margin-left: auto;" @click="getCode">{{getCodeText}}</text>
			</view>
			<view class="login-button text-base" style="font-weight: 800;" @click="nextStep">
				下一步
			</view>	
		</view>
		<uni-popup ref="alertDialog" type="dialog" @maskClick="$refs.alertDialog.close()">
			<uni-popup-dialog type="warning" title="客服电话" :buttonArr="['取消','拨打']" :content="phoneNumber" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber:"",
				getCodeText:"获取验证码",
				getCodeDisabled:false,
				phone:"",
				code:""
			};
		},
		created() {
			this.phoneNumber = this.$util.PHONE_NUMBER;
		},
		onShow() {
			this.phone = this.$api.prePage().userInfo.phonenumber
			this.userInfo = this.$api.prePage().userInfo
		},
		methods:{
			dialogConfirm() {
				console.log('拨打电话')
				uni.makePhoneCall({
				    phoneNumber: this.phoneNumber.replace(/\s+/g,"-")  //仅为示例
				});
			},
			dialogClose() {
			},
			nextStep(){
				console.log(this.code)
				this.$util.doPostJson('/user/VerifyPhoneOwner', {code: this.code}, {}, true).then(res => {
					if(res.data.code == this.$ResponseStatus.OK){
						if(this.userInfo.loginType === 'apple'){
							uni.navigateTo({
								url:'/pages/login/binding-phone/binding-phone?type='+this.userInfo.loginType
							})
						}else {
							uni.navigateTo({
								url:"/pages/user/my-setting/update-Phone/update-Phone"
							})
						}
					}
				}).catch(error => {
					console.log(error)
				})
				
			},
			getCode() {
				if(this.getCodeDisabled){
					return
				}
				if (!this.$util.isPhone(this.phone)) {
					this.$util.showInfoToast('请输入正确的手机号')
					return
				}
				this.getCodeDisabled = true
				
				let self = this
				let time = 60
				this.$util.doPostForm('/loginApi/SendSMS', {phoneNumber: this.phone}, {}, false).then(res => {
					if(res.data.code == this.$ResponseStatus.ERROR){
						this.getCodeDisabled = false
					}else {
						let set = setInterval(()=> {
							self.getCodeText = '('+ --time +')秒后重新获取'
						}, 1000);
						this.time1 = setTimeout(()=> {
							self.getCodeDisabled = false;
							self.getCodeText = '获取验证码'
							clearInterval(set)
						}, time * 1000)
					}
					
				}).catch(error => {
					console.log(error)
				})
			}
		},
		onNavigationBarButtonTap(e) {
			 this.$refs.alertDialog.open()
		}
	}
</script>

<style lang="scss">
	.font-color{
		color: #6BA9D7;
	}
	
	.font-color1{
		color: #919191;
	}
	
	.container{
		padding: 30rpx 50rpx;
		margin-top: 80rpx;
		
		.input-phone{
			padding: 15rpx 0rpx;
			align-items: center;
			margin-top: 76rpx;
		}
		
		.login-button{
			flex: 1;
			background: $index-bg-bottom;
			color: #ffffff;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			border-radius: 50rpx;
			margin-top: 50rpx;
		}
		
	}
	
	.text-lg{
		font-size: 48rpx;
	}
	
	.text-base{
		font-size: 34rpx;
	}
</style>
